//
//  SAMKeychain+MDHPTUtil.m
//  MMWPT
//
//  Created by 高得华 on 2018/9/4.
//  Copyright © 2018年 高得华. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "SAMKeychain+MDHPTUtil.h"

/// 登录账号的key
static NSString *const MMWPT_RAW_LOGIN = @"MMWPT_RAW_LOGIN";
static NSString *const MMWPT_SERVICE_NAME_IN_KEYCHAIN = @"";
static NSString *const MMWPT_DEVICEID_ACCOUNT = @"DeviceID";
static NSString *const MMWPT_DEVICEID_UserInfo = @"UserInfo";

@implementation SAMKeychain (MDHPTUtil)

+ (NSString *)rawLogin {
    return [[NSUserDefaults standardUserDefaults] objectForKey:MMWPT_RAW_LOGIN];
}

+ (BOOL)setRawLogin:(NSString *)rawLogin {
    if (rawLogin == nil) NSLog(@"+setRawLogin: %@", rawLogin);
    [[NSUserDefaults standardUserDefaults] setObject:rawLogin forKey:MMWPT_RAW_LOGIN];
    [[NSUserDefaults standardUserDefaults] synchronize];
    return YES;
}

+ (BOOL)deleteRawLogin {
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:MMWPT_RAW_LOGIN];
    [[NSUserDefaults standardUserDefaults] synchronize];
    return YES;
}

+ (NSString *)deviceId {
    NSString * scheme = kMDHInfoPlist().CFBundleURLScheme;
    if (scheme.length <= 0) {
        scheme = @"huamei_app";
    }
    NSString * deviceidStr = [SAMKeychain passwordForService:scheme account:MMWPT_DEVICEID_ACCOUNT];
    if (deviceidStr == nil) {
        deviceidStr = [UIDevice currentDevice].identifierForVendor.UUIDString;
        [SAMKeychain setPassword:deviceidStr forService:scheme account:MMWPT_DEVICEID_ACCOUNT];
    }
    return deviceidStr;
}

///保存信息
+ (void)saveUserInfo:(NSString *)data {
    if (data) {
        NSString * scheme = kMDHInfoPlist().CFBundleURLScheme;
        if (scheme.length <= 0) {
            scheme = @"huamei_app";
        }
        [SAMKeychain setPassword:data forService:scheme account:MMWPT_DEVICEID_UserInfo];
    }
}
///获取用户信息
+ (NSString *)userInfo {
    NSString * scheme = kMDHInfoPlist().CFBundleURLScheme;
    if (scheme.length <= 0) {
        scheme = @"huamei_app";
    }
    NSString * info = [SAMKeychain passwordForService:scheme account:MMWPT_DEVICEID_UserInfo];
    if (info == nil) {
        info = @"";
    }
    return info;
}

@end
